//
//  HomeVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/3.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "HomeVC.h"
#import <Masonry.h>
#import "HomeCell.h"
#import "HomeKLineDetailVC.h"
#import "SGAdvertScrollView.h"
#import "TYCyclePagerView.h"
#import "HomeBannerCell.h"
#import "TYPageControl.h"
#import "HomeModel.h"
@interface HomeVC ()<UINavigationControllerDelegate,UITableViewDelegate, UITableViewDataSource,TYCyclePagerViewDataSource,TYCyclePagerViewDelegate>{
    UIView *_centetView;
    UITableView *_homeTbl;
    NSMutableArray *_dataArr; //列表的数组
}
@property (strong, nonatomic)SGAdvertScrollView *advertScrollViewTop;
@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;
@property (nonatomic, strong) NSArray *datas;
@end

@implementation HomeVC

- (void)loadData {
    NSMutableArray *datas = [NSMutableArray array];
    for (int i = 0; i < 7; ++i) {
        if (i == 0) {
            [datas addObject:[UIColor redColor]];
            continue;
        }
        [datas addObject:[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0]];
    }
    _datas = [datas copy];
    _pageControl.numberOfPages = _datas.count;
    [_pagerView reloadData];
    //[_pagerView scrollToItemAtIndex:3 animate:YES];
}

- (void)_initView{
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
//    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[HomeBannerCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.view addSubview:pagerView];
    _pagerView = pagerView;
    
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    //pageControl.numberOfPages = _datas.count;
    pageControl.currentPageIndicatorSize = CGSizeMake(6, 6);
    pageControl.pageIndicatorSize = CGSizeMake(12, 6);
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = BaseColor;
    //    pageControl.pageIndicatorImage = [UIImage imageNamed:@"Dot"];
    //    pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"DotSelected"];
    pageControl.contentInset = UIEdgeInsetsMake(0, 0, 0, 20);
    pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    //    pageControl.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //    [pageControl addTarget:self action:@selector(pageControlValueChangeAction:) forControlEvents:UIControlEventValueChanged];
    [_pagerView addSubview:pageControl];
    _pageControl = pageControl;
    
    
    _centetView = [[UIView alloc] init];
    _centetView.backgroundColor = [UIColor whiteColor];
    _centetView.layer.cornerRadius = 2.f;
    [self.view addSubview:_centetView];
    
    self.advertScrollViewTop = [[SGAdvertScrollView alloc] init];
    _advertScrollViewTop.titles = @[@"关于中融币上线事宜，请注意查收", @"中融币上线，充值送好礼", @"中融币上线，在线充值送好礼，请大家积极参与参与请大家积极参与参与"];
    _advertScrollViewTop.titleColor = [UIColor colorFromHexRGB:@"333333"];
    [_centetView addSubview:self.advertScrollViewTop];
    
    [self _setMasonry];
    
    [self loadData];
}
- (void)_setMasonry{
    __weak typeof(self) weakSelf = self;
    
    UIImageView *ringImage = [[UIImageView alloc] init];
    ringImage.image = [UIImage imageNamed:@"xiaoxi"];
    [_centetView addSubview:ringImage];
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    
    UIView *leftBlueView = [UIView new];
    leftBlueView.backgroundColor = BaseColor;
    [headerView addSubview:leftBlueView];
    
    UILabel *headerLbl = [UILabel new];
    headerLbl.text = @"涨幅榜";
    headerLbl.font = [UIFont systemFontOfSize:20];
    [headerView addSubview:headerLbl];
    
    _homeTbl = [[UITableView alloc] init];
    _homeTbl.delegate = self;
    _homeTbl.dataSource = self;
    _homeTbl.tableFooterView = [UIView new];
    _homeTbl.rowHeight = 60.f;
    [_homeTbl registerNib:[UINib nibWithNibName:@"HomeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HomeCellCell"];
    [self.view addSubview:_homeTbl];
    
    [_pagerView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.top).offset(-20);
        make.height.equalTo(@180);
    }];
    [_pageControl makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.pagerView.right);
        make.bottom.equalTo(weakSelf.pagerView.bottom);
        make.height.equalTo(@20);
        make.width.equalTo(weakSelf.view.width);
    }];
    [_centetView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.pagerView.bottom).offset(10);
        make.height.equalTo(@40);
        make.left.right.equalTo(self.view).offset(10);
        make.right.equalTo(self.view.right).offset(-10);
    }];
    [ringImage makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_centetView.left).offset(5);
        make.centerY.equalTo(self->_centetView.centerY);
        make.width.width.height.equalTo(@20);
    }];
    [self.advertScrollViewTop makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self->_centetView).insets(UIEdgeInsetsMake(0, 30, 0, 0));
    }];
    
    [headerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_centetView.bottom).offset(10);
        make.height.equalTo(@50);
        make.left.right.equalTo(self->_centetView);
    }];
    
    [leftBlueView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView);
        make.height.equalTo(@20);
        make.width.equalTo(@7);
        make.left.equalTo(headerView.left).offset(5);
    }];
    [headerLbl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftBlueView.right).offset(5);
        make.centerY.equalTo(leftBlueView.centerY);
    }];
    
    [_homeTbl makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.bottom);
        make.left.right.equalTo(headerView);
        make.bottom.equalTo(weakSelf.view.bottom);
    }];
}
#pragma mark - 导航栏的代理
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isPushSelfClass = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isPushSelfClass animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"F1F2F3"];
    _gjfb = 1.0;
    [self _initView];
    
}

#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HomeCellCell";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[HomeCell alloc] init];
    }
    HomeModel *model = _dataArr[indexPath.row];
    cell.gjfb = _gjfb;
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HomeKLineDetailVC *vc = [HomeKLineDetailVC new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return _datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    HomeBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
//    cell.backgroundColor = _datas[index];
    cell.banner = @"RPICTest";
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 0;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestData];
    
}

- (void)requestData{
    
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/main/index"];
    [NetRequestTool post:url
                  params:@{}
                 success:^(id  _Nonnull json) {
                     
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         NSDictionary *dic = [json objectForKey:@"data"];
                         NSArray *array = [dic  objectForKey:@"coinList"];
                         self->_dataArr = [HomeModel mj_objectArrayWithKeyValuesArray:array];
                         [self->_homeTbl reloadData];
                         
                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
    
    NSString *url2 = [NSString stringWithFormat:@"http://119.160.234.147:8080/main/getPrice"];
    [NetRequestTool post:url2
                  params:@{}
                 success:^(id  _Nonnull json) {
                     
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         
                         NSDictionary *dic = [json objectForKey:@"data"];
                         NSString *gjfbStr = [dic objectForKey:@"gjfb"];
                         _gjfb = [gjfbStr floatValue];
                         [self->_homeTbl reloadData];

                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
    
}

@end
